<template>
	<xn-form-container
		:title="formData.id ? '编辑' : '新增'"
		width="1000"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form
			:wrapper-col="{ span: 16 }"
			:label-col="{ span: 8 }"
			autocomplete="off"
			labelAlign="right"
			ref="formRef"
			:model="formData"
			:rules="formRules"
		>
			<a-row>
				<a-col :span="12">
					<a-form-item label="仓库名称" name="warehouseName">
						<a-input v-model:value="formData.warehouseName" placeholder="请输入仓库名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="仓库编号" name="warehouseNum">
						<a-input v-model:value="formData.warehouseNum" placeholder="请输入仓库编号" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="联系人" name="contacts">
						<a-input v-model:value="formData.contacts" placeholder="请输入联系人" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="手机号" name="phone">
						<a-input :maxlength="11" v-model:value="formData.phone" placeholder="请输入手机号" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="地区" name="area">
						<a-input v-model:value="formData.area" placeholder="请输入地区" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="仓库地址" name="warehouseAddr">
						<a-input v-model:value="formData.warehouseAddr" placeholder="请输入仓库地址" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="仓库人数" name="num">
						<a-input v-model:value="formData.num" placeholder="请输入仓库人数" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="仓库状态">
						<a-select :getPopupContainer="trigger => trigger.parentNode" v-model:value="formData.status" placeholder="请选择仓库状态">
							<a-select-option key="1" value="启用"></a-select-option>
							<a-select-option key="0" value="未启用"></a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
			</a-row>
			<a-row>
				<a-col :span="12">
					<a-form-item label="备注">
						<a-textarea
							:autosize="{ minRows: 4, maxRows: 4 }"
							v-model:value="formData.remark"
							placeholder="请输入备注"
						/>
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">提交</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="productForm">
	import { required, rules } from '@/utils/formRules'
	import storeCenterApi from '@/api/base_data/storeCenterApi'
	// 定义emit事件
	//PROCUREMENT_PARTNERS
	const emit = defineEmits({ successful: null })
	// 默认是关闭状态
	const visible = ref(false)
	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			formData.value = Object.assign({}, record)
		} else {
			formData.value = {
				status: '启用'
			}
		}
	}

	// 关闭抽屉
	const onClose = () => {
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		warehouseName: [required('请输入仓库名称')],
		warehouseNum: [required('请输入仓库编号')],
		warehouseAddr: [required('请输入仓库地址')],
		contacts: [required('请输入联系人')],
		phone: [required('请输入手机号'), rules.phone],
		num: [required('请输入仓库人数'), rules.number],
		area: [required('请输入地区')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				storeCenterApi
					.addOrEdit(formData.value.id ? 'edit' : 'add', formData.value)
					.then(() => {
						visible.value = false
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>

<style scoped :type="less">
	.childAddButton {
		margin-bottom: 10px;
	}
</style>
